package com.maila.xiaoaoschool.widget;


import android.app.Activity;
import android.content.Context;
import android.util.AttributeSet;
import android.util.DisplayMetrics;
import android.util.TypedValue;
import android.view.LayoutInflater;
import android.view.View;
import android.view.animation.LinearInterpolator;
import android.view.animation.TranslateAnimation;
import android.widget.FrameLayout;
import android.widget.HorizontalScrollView;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.RadioGroup.OnCheckedChangeListener;

import com.maila.xiaoaoschool.R;


public class MyRadioHorizontalScrollView extends HorizontalScrollView {

    private Activity mContext;
    private LayoutInflater mInflater;
    private View view;
    private int windowWitdh;
    private int count;
    private int indicatorWidth;
    private RadioGroup rg_nav_content;

    private int currentIndicatorLeft = 0;
    private int scrollX;
    Context context;
    CallBack callback;

    public RadioGroup getRg_nav_content() {
        return rg_nav_content;
    }

    public void setRg_nav_content(RadioGroup rg_nav_content) {
        this.rg_nav_content = rg_nav_content;
    }

    public MyRadioHorizontalScrollView(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        // TODO Auto-generated constructor stub
    }

    public MyRadioHorizontalScrollView(Context context, AttributeSet attrs) {
        super(context, attrs);
        // TODO Auto-generated constructor stub
        this.context = context;
    }

    public MyRadioHorizontalScrollView(Context context) {
        super(context);
        // TODO Auto-generated constructor stub
    }

    public void setSomeParam(String[] tabTitle, int count, Activity context) {
        this.mContext = context;
        this.removeAllViews();
        // this.view = view;
        mInflater = LayoutInflater.from(context);
        this.view = mInflater.inflate(R.layout.sync_hsv_item, null);
        this.addView(view);

        DisplayMetrics dm = new DisplayMetrics();
        context.getWindowManager().getDefaultDisplay().getMetrics(dm);
        windowWitdh = dm.widthPixels;

        this.count = count;
        indicatorWidth = (windowWitdh - (int) (50 * (dm.density))) / count;
        init(tabTitle);
    }

    private void init(String[] tabTitle) {
        // TODO Auto-generated method stub
        rg_nav_content = (RadioGroup) view.findViewById(R.id.rg_nav_content);

//        initIndicatorWidth();
        initNavigationHSV(tabTitle);
        setListener();

    }

    private void setListener() {
        // TODO Auto-generated method stub
        rg_nav_content.setOnCheckedChangeListener(new OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId) {

                if (rg_nav_content.getChildAt(checkedId) != null) {
                    moveAnimation(checkedId);
                    if (callback != null) {
                        callback.onradiobuttoncheck(checkedId);
//						rg_nav_content.getChildAt(checkedId).setBackgroundResource(R.drawable.bg_radio);
                    }
                    // 跟随一起 切换
                }
            }
        });
    }

    public void moveAnimation(int checkedId) {
        TranslateAnimation animation = new TranslateAnimation(currentIndicatorLeft, indicatorWidth * checkedId, 0f, 0f);
        animation.setInterpolator(new LinearInterpolator());
        animation.setDuration(100);
        animation.setFillAfter(true);

        // 执行位移动画
//        iv_nav_indicator.startAnimation(animation);

        // 记录当前 下标的距最左侧的 距离
        currentIndicatorLeft = indicatorWidth * checkedId;
        scrollX = (checkedId > 1 ? currentIndicatorLeft : 0) - indicatorWidth * 2;
        this.post(runnable);
    }

    private Runnable runnable = new Runnable() {

        @Override
        public void run() {
            // TODO Auto-generated method stub
            smoothScrollTo(scrollX, 0);
        }
    };

    private void initNavigationHSV(String[] tabTitle) {
        // TODO Auto-generated method stub
        rg_nav_content.removeAllViews();
        DisplayMetrics dm = new DisplayMetrics();
        ((Activity)context).getWindowManager().getDefaultDisplay().getMetrics(dm);
        float pidwidth=dm.widthPixels;
        for (int i = 0; i < tabTitle.length; i++) {
            RadioButton rb = (RadioButton) mInflater.inflate(R.layout.sync_nav_radiogroup_item, null);
            if (i == 0) {
                rb.setChecked(true);
            }
            rb.setId(i);
            rb.setText(tabTitle[i]);
            switch ((int) pidwidth){
                case  720:
                    rb.setTextSize(TypedValue.COMPLEX_UNIT_PX,28);
                    rb.setPadding(24,0,24,0);
                    break;
                case 480:
                     rb.setTextSize(TypedValue.COMPLEX_UNIT_PX,18);
                    rb.setPadding(14,0,14,0);
                    break;
                case 1080:
                    rb.setTextSize(TypedValue.COMPLEX_UNIT_PX,42);
                    rb.setPadding(36,0,36,0);
                    break;
                case 1536:
                    rb.setTextSize(TypedValue.COMPLEX_UNIT_PX,65);
                    rb.setPadding(52,0,52,0);
                    break;
                case 1440:
                    rb.setTextSize(TypedValue.COMPLEX_UNIT_PX,60);
                    rb.setPadding(68,0,68,0);
                    break;
                default:
                    rb.setTextSize(17);
                    rb.setPadding(27,0,27,0);
                    break;
            }
            if(tabTitle.length>5){
                FrameLayout.LayoutParams parms= new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.MATCH_PARENT);
                parms.setMargins(20,0,20,0);
                rb.setLayoutParams(parms);
            } else if(tabTitle.length==1){
                rb.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT));
            }
            else {
                FrameLayout.LayoutParams parms= new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.MATCH_PARENT);
                parms.setMargins(8,0,8,0);
                rb.setLayoutParams(parms);
                rb.setLayoutParams(new LayoutParams(indicatorWidth, LayoutParams.MATCH_PARENT));
            }
            rg_nav_content.addView(rb);
        }
        RadioButton rb = (RadioButton) mInflater.inflate(R.layout.sync_nav_radiogroup_item, null);
        rg_nav_content.addView(rb);
    }

//    private void initIndicatorWidth() {
//        // TODO Auto-generated method stub
////        ViewGroup.LayoutParams cursor_Params = iv_nav_indicator.getLayoutParams();
////        cursor_Params.width = indicatorWidth;
////        iv_nav_indicator.setLayoutParams(cursor_Params);
//    }

    public CallBack getCallback() {
        return callback;
    }

    public void setCallback(CallBack callback) {
        this.callback = callback;
    }

    public interface CallBack {
        void onradiobuttoncheck(int checkid);
    }

}
